﻿using AIVision.Inspect.Halcon;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XRVision.DLCheckUnit
{
   public  class DLInspect
    {
        public DLInspect(string processFilePath,string modelFilePath,HTuple dLDevice)
        {
            this.processFilePath = processFilePath;
            this.modelFilePath = modelFilePath;
            this.dLDevice = dLDevice;
        }
        private string processFilePath { get; set; } 
        private string modelFilePath { get; set; }
        private HTuple dLDevice;

        HTuple BatchSizeInference = 1;
        HTuple DLModelHandle = new HTuple();
        HTuple DLPreprocessParam = new HTuple();

      

        private HTuple dLSampleBatch = new HTuple();
        public HTuple DLSampleBatch { get => dLSampleBatch; }

        private HTuple dLResultBatch = new HTuple();
        public HTuple DLResultBatch { get => dLResultBatch; }
        

        private HObject inferenceImage = new HObject();
        public HObject InferenceImage { get => inferenceImage; }

        private HTuple objNames;
        public HTuple ObjNames { get => objNames; }

        private HObject objRange = new HObject();
        public HObject ObjRange { get => objRange; }

        public void Init()
        {
            DLModelHandle.Dispose();
            DLPreprocessParam.Dispose();

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                HOperatorSet.SetSystem("cudnn_deterministic","true");
                HOperatorSet.ReadDlModel(modelFilePath, out DLModelHandle);
                HOperatorSet.SetDlModelParam(DLModelHandle, "batch_size", BatchSizeInference);
                HOperatorSet.SetDlModelParam(DLModelHandle, "device", dLDevice);
                HOperatorSet.ReadDict(processFilePath, null, null, out DLPreprocessParam);
            }
        }

        public void ExInspect(HObject img)
        {
            HObject defectRegion = new HObject();
         
            dLSampleBatch.Dispose();
            dLResultBatch.Dispose();
            defectRegion.Dispose();
            objRange.Dispose();
            DLDatasetHelper.gen_dl_samples_from_images(img, out dLSampleBatch);
            DLPreprocessingHelper.preprocess_dl_samples(DLSampleBatch, DLPreprocessParam);
            HOperatorSet.ApplyDlModel(DLModelHandle, DLSampleBatch, new HTuple(), out dLResultBatch);
            HOperatorSet.GetDictTuple(dLResultBatch, "bbox_class_name", out objNames);
            HOperatorSet.GetDictObject(out inferenceImage, DLSampleBatch, "image");
            HTuple row1, col1, row2, col2;
            HOperatorSet.GetDictTuple(dLResultBatch, "bbox_row1", out row1);
            HOperatorSet.GetDictTuple(dLResultBatch, "bbox_col1", out col1);
            HOperatorSet.GetDictTuple(dLResultBatch, "bbox_row2", out row2);
            HOperatorSet.GetDictTuple(dLResultBatch, "bbox_col2", out col2);
            HOperatorSet.GenRectangle1(out objRange, row1, col1, row2, col2);
        }

        public void Stop()
        {
            DLModelHandle = new HTuple();
            DLModelHandle.Dispose();
            DLPreprocessParam = new HTuple();
            DLPreprocessParam.Dispose();
        }
    }
}
